package tuples;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Used for storing one element from a Hashtable<String, ArrayList<String>>.
 * 
 * @author Shimu
 *
 */
public class StringListPair {

	private String key;
	private List<String> value;

	public StringListPair(String key, List<String> value) {
		this.key = key;
		this.value = value;
	}

	public String getKey(){
		return key;
	}
	
	public List<String> getValue(){
		return value;
	}
	
	/*
	public String getValue(){
		String str = "";
		for (String element:value){
			str += element + '\n';
		}
		
		return str;
	}
	*/
	
	/**
	 * Sorts the value (which is an ArrayList) of this StringListPair alphabetically.
	 */
	public void sortValue() {
		Collections.sort(value, String.CASE_INSENSITIVE_ORDER);
	}

	/**
	 * Sorts this List<StringListPair> alphabetically by comparing each StringListPair's key.
	 * 
	 * @param array
	 */
	public static void sortByKey(List<StringListPair> array) {
		/**
		 * Compare by keys		
		 */
		final class CompareByKey implements Comparator<StringListPair> {

			public CompareByKey() {
			}

			@Override
			public int compare(StringListPair pair1, StringListPair pair2) {

				return String.CASE_INSENSITIVE_ORDER.compare(pair1.key,
						pair2.key);
				//return pair1.key.compareTo(pair2.key);
			}
		}

		Collections.sort(array, new CompareByKey());
	}

	/**
	 * Sorts this List<StringListPair> alphabetically by comparing each StringListPair's key.
	 * Then each StringListPair's value is also sorted alphabetically.
	 * 
	 * @see sortValue
	 * @param array
	 */
	public static void sortAll(List<StringListPair> array) {
		sortByKey(array);
		for (StringListPair pair : array) {
			pair.sortValue();
		}
	}

	/**
	 * Returns the String representation of this StringListPair. 
	 * The returned String takes the form:
	 * "(key, [element1, element2, ... elementn])"
	 * 
	 */
	@Override
	public String toString() {
		return ("(" + key + ", " + value + ")");
	}
}